package com.jade.framework.base.security;

import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.util.Enumeration;

/**
 * 抽象的安全策略.
 *
 * @author dwzhang
 * @version 1.0 13-4-18 上午9:26
 */
public abstract class AbstractSecurityPolicy implements SecurityPolicy
{
    /**
     * 合并权限集合（用于不同逻辑的合并扩展）
     *
     * @param pc1 PermissionCollection
     * @param pc2 PermissionCollection
     * @return 合并之后权限集合
     */
    public PermissionCollection merge(PermissionCollection pc1, PermissionCollection pc2)
    {
        if (pc1 == null) {
            return pc2;
        }
        else if (pc2 == null) {
            return pc1;
        }
        else {
            Permissions perms = null;
            for (Enumeration e = pc1.elements(); e.hasMoreElements(); ) {
                if (perms == null) {
                    perms = new Permissions();
                }
                perms.add((Permission)e.nextElement());
            }
            for (Enumeration e = pc2.elements(); e.hasMoreElements(); ) {
                if (perms == null) {
                    perms = new Permissions();
                }
                perms.add((Permission)e.nextElement());
            }
            return perms;
        }
    }
}
